package com.esp.commentdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.esp.commentdemo.util.ResultGenerator;
import com.esp.commentdemo.dao.NewsCommentMapper;
import com.esp.commentdemo.entity.NewsComment;
import com.esp.commentdemo.service.CommentService;
import com.esp.commentdemo.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/admin")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Resource
    private NewsCommentMapper newsCommentMapper;


    @GetMapping("/comments/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {

        if (ObjectUtils.isEmpty(params.get("page")) || ObjectUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(commentService.getCommentsPage(pageUtil));
    }

    //查询评论内容
    @GetMapping("/comments/content/{newsId}")
    @ResponseBody
    public String getCommentCount(@PathVariable Long newsId) {
        LambdaQueryWrapper<NewsComment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsComment::getNewsId, newsId);
        List<NewsComment> comments = newsCommentMapper.selectList(wrapper);
        if (comments.size() != 0) {
            String commentBody = comments.get(0).getCommentBody();
            return commentBody;
        }else {
            return null;
        }
    }

    @PostMapping("/comments/add")
    @ResponseBody
    public void addComment(@RequestParam Long newsId,
                           @RequestParam String commentator,
                           @RequestParam String commentBody) {
        NewsComment comment = new NewsComment();
        comment.setNewsId(newsId);
        comment.setCommentator(commentator);
        comment.setCommentBody(commentBody);
        commentService.addComment(comment);
    }


    @PostMapping("/comments/checkDone")
    @ResponseBody
    public Result checkDone(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (commentService.checkDone(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("审核失败");
        }
    }

    @PostMapping("/comments/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (commentService.deleteBatch(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("刪除失败");
        }
    }

    @GetMapping("/comments")
    public String list(HttpServletRequest request) {
        request.setAttribute("path", "comments");
        return "admin/comment";
    }


    //点赞
    @GetMapping("/comments/thumbsup/{newsId}")
    @ResponseBody
    public Result thumbsUp(@PathVariable Long newsId) {
        return  ResultGenerator.genSuccessResult(commentService.thumbsUp(newsId));
    }
    //取消点赞
    @GetMapping("/comments/thumbsdown/{newsId}")
    @ResponseBody
    public Result thumbsDown(@PathVariable Long newsId) {
        return  ResultGenerator.genSuccessResult(commentService.thumbsDown(newsId));
    }
    //查询点赞
    @GetMapping("/comments/getnum/{newsId}")
    @ResponseBody
    public Result getThumbsdNum(@PathVariable Long newsId) {
        return  ResultGenerator.genSuccessResult(commentService.getThumbsdNum(newsId));
    }

}
